<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/26 0026
 * Time: 14:35
 */

namespace HiCommon\Service;

use HiCommon\Repository\DisclaimerRepository;
use HiCommon\Repository\LogRepository;
use HiCommon\Repository\OrderCheckpointVehicleHelpdrivingRepository;
use Modules\Admin\Service\YearCheckNoticeService;

class CommonService extends BaseService
{
    /**
     * 计算两个地理点的距离
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_location_distance($arr_input){
        $s_start_longitude = str_trim($arr_input['start_longitude']);//起点经度
        $s_start_latitude = str_trim($arr_input['start_latitude']);//起点纬度
        $s_end_longitude = str_trim($arr_input['end_longitude']);//终点经度
        $s_end_latitude = str_trim($arr_input['end_latitude']);//终点纬度
        if(empty($s_start_longitude)){
            return hello_error('起点经度不能为空');
        }
        if(empty($s_start_latitude)){
            return hello_error('起点纬度不能为空');
        }
        if(empty($s_end_longitude)){
            return hello_error('终点经度不能为空');
        }
        if(empty($s_end_latitude)){
            return hello_error('终点纬度不能为空');
        }

        //求两个已知经纬度之间的距离
        $res = location_distance($s_start_longitude,$s_start_latitude,$s_end_longitude,$s_end_latitude);
        return  hello_success('成功',$res);
    }

    /**
     * 呼叫代驾信息回调
     *
     * @param $arr_input
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_appointment_order_helpdriving_callback($arr_input){
        $s_order_sn = string_trim($arr_input['order_sn']);
        $s_helpdriving_order_id = intval($arr_input['helpdriving_order_id']);
        $s_helpdriving_phone = string_trim($arr_input['helpdriving_phone']);
        if(strlen($s_order_sn) <= 0){
            return hello_error('养车订单号不能为空');
        }
        if($s_helpdriving_order_id <= 0){
            return hello_error('代驾订单id不能为空');
        }
        if(strlen($s_helpdriving_phone) <= 0){
            return hello_error('乘客手机号不能为空');
        }
        if(strlen($s_helpdriving_phone) != 11){
            return hello_error('乘客手机号长度错误');
        }

        $arr_where = ['order_sn'=>$s_order_sn];
        $arr_data = ['helpdriving_order_id'=>$s_helpdriving_order_id,'helpdriving_phone'=>$s_helpdriving_phone];
        $res = OrderCheckpointVehicleHelpdrivingRepository::edit_by_where($arr_where,$arr_data);
        if($res === 0){
            return hello_error('养车订单号错误');
        }
        return  hello_success('回调成功');
    }

    /**
     * 今晨代驾订单详情
     *
     * @param $n_id
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function jinchen_helpdriving_order_detail($n_id){
        if($n_id <= 0){
            return hello_error('订单ID不能为空');
        }

        //查询代驾信息
        $arr_helpdriving_order = OrderCheckpointVehicleHelpdrivingRepository::get_one($n_id,['id','helpdriving_order_id','helpdriving_phone'])->toArray();
        $n_helpdriving_id = intval($arr_helpdriving_order['helpdriving_order_id']);
        $n_helpdriving_phone = $arr_helpdriving_order['helpdriving_phone'];
        if(empty($arr_helpdriving_order)){
            return hello_error('数据不存在');
        }
        if(empty($n_helpdriving_id)){
            return hello_error('暂无代驾信息-1');
        }
        if(empty($n_helpdriving_phone)){
            return hello_error('暂无代驾信息-2');
        }

        //根据用户手机获取今晨代驾的token
        $s_url = 'http://jcdj.chxing.cn/getway/api';
        $data = [
          'method'=>'getTokenByPhone',
          'service'=>'accessSystem',
          'args'=>
            [
              [ 'deviceType'=>'miniProgram',
                'loginType'=>'1',
                'loginUsername'=>$n_helpdriving_phone
              ]
            ]
        ];
        $data = json_encode($data);
        $headers = ["Content-Type: application/json"];
        $res = json_decode(curl_post($s_url,$data,$headers),true);
        if($res['code'] != 200){
            return hello_error('今晨代驾-'.$res['message']);
        }
        else{
            $token = $res['data'];
        }

        //根据token和今晨代驾订单id获取订单信息
        $s_url = 'http://jcdj.chxing.cn/getway/api';
        $data = [
          'method'=>'getOrderDetailsByOrderId',
          'service'=>'orderApi',
          'version'=>'',
          'args'=> [$n_helpdriving_id]
        ];
        $data = json_encode($data);
        $headers = ["Content-Type: application/json","token: ".$token];
        $res = json_decode(curl_post($s_url,$data,$headers),true);
        if($res['code'] != 200){
            return hello_error('今晨代驾-'.$res['message']);
        }
        else{
            return hello_success('成功',$res['data']);
        }

    }

    /**
     * 年检须知
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_year_check_note($arr_option = []){
        return  YearCheckNoticeService::get_all($arr_option);
    }

    /**
     * 免责声明
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function disclaimer(){
        $arr_option['field'] = ['id','content','created_at','updated_at','deleted_at'];
        $arr_option['order'] = ['key'=>'sort' , 'value'=>'desc'];
        $res = \HiCommon\Repository\ProtocolRepository::get_one_by_where( (array) ['type'=>10 , 'status'=>1] , (array) $arr_option );
        if( empty( $res->first() ) ){
            return hello_success('ok', [] );
        }
        $arr_disclaimer = $res->toarray();
        $arr_disclaimer['content'] = strip_tags( $arr_disclaimer['content'] );   //删除字符串中是html标签
        return  hello_success('成功',$arr_disclaimer);
    }

    /**
     * 发送手机验证码接口
     *
     * @param string $s_phone
     *
     * @return array
     * @throws \Exception
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function api_send_phonecode($s_phone = ''){
        $res = send_phone_code( $s_phone);
        if($res['state'] == 0){
            $code = redis_phone_code( $s_phone );
            $arrLog['phone'] = $s_phone;
            $arrLog['code'] = $code;

            //添加验证码记录
            LogRepository::record_phone_code($arrLog);
        }
        return $res;
    }



}